/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package gui;

import Presenter.Presenter;
import javax.swing.JOptionPane;
import model.RemoteUser;

/**
 *
 * @author yorrickbarnay
 */
public class GUIFacade implements FromPresenter{
    
    private Window w;
    public GUIFacade(Presenter p){
        w = new Window(p);
    }
    @Override
    public void processRemoteUserConnection(RemoteUser remote_user){
        //On ajoute cet utilisateur dans la liste des remotes_users
        w.remote_users_list.addElement(remote_user);
        
        String to_display = "******************** " +
                remote_user.toString() + " just logged in ********************" ;
        if (w.messages_pane.getText().equals(""))
            w.messages_pane.setText(w.messages_pane.getText() + to_display);
        else
            w.messages_pane.setText(w.messages_pane.getText() + "\n" + to_display);
    };
    
    @Override
    public void processRemoteUserDeconnection(RemoteUser remote_user){
        //
        String to_display = "******************** ["+remote_user.toString()+"]"+ " just logged out and is no longer available ********************";
        w.messages_pane.setText(w.messages_pane.getText() + "\n" + to_display);
        
        //supprimer l'utilisateur de remote_users_list
        w.remote_users_list.removeElement(remote_user);

    };
    
    @Override
    public void processMessageReception(RemoteUser remote_user, String text_message){
        //ajouter le message dans messages_pane
         String to_display = "From [" + remote_user.toString() + "] : "+text_message;

        w.messages_pane.setText(w.messages_pane.getText() + "\n" + to_display);
    };
    
  // @Override
    public void processFileReception(RemoteUser remote_user, String file_name,boolean accord){
        //ajouter le message dans messages_pane
        String to_display;
        if (accord){
          to_display = "Dowload file from [" + remote_user.toString() + "] : "+file_name;
        }
        else{
            to_display = "Refuse file from [" + remote_user.toString() + "] : "+file_name;
        }
        w.messages_pane.setText(w.messages_pane.getText() + "\n" + to_display);
    };
    
    public void processFileSending(RemoteUser remote_user, String file_name,boolean accord){
        //ajouter le message dans messages_pane
        String to_display;
        if (accord){
         to_display = "File sent to [" + remote_user.toString() + "] : "+file_name;
        }else {to_display = "File refused by [" + remote_user.toString() + "] : "+file_name;
        }
        
        w.messages_pane.setText(w.messages_pane.getText() + "\n" + to_display);
    };
    @Override
    public boolean processRemoteUserFileRequest(RemoteUser remote_user, String file_name) {
        int n = JOptionPane.showConfirmDialog(
                this.w, 
                "Voulez-vous que " + remote_user.toString() + " vous envoie le fichier " + file_name, 
                "Demande d'envoie d'un fichier",
                JOptionPane.YES_NO_OPTION);
        
        return n == JOptionPane.YES_OPTION;   
    }    
}
